﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Pg.BioMedics.SDR.Diagnostics
{
    /// <summary>
    /// An instantaneous counter that shows the most recently observed value.
    /// </summary>
    [DebuggerDisplay("Category : [{Category}] Name : [{Name}] Type [{Type}] Value [{value}]")]
    public class NumberOfItemsPerformanceCounter : BasePerformanceCounter
    {
        #region Fields

        private long value = 0;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates instance of number of items counter. 
        /// </summary>
        /// <param name="categoryName">Perfoamce counter category name</param>
        /// <param name="counterName">Performance counter name</param>
        internal NumberOfItemsPerformanceCounter(string categoryName, string counterName)
            : base(categoryName, counterName, PerformanceCounterType.NumberOfItems64)
        { }

        #endregion

        #region Methods

        /// <summary>
        /// Sets counter value and updates windows performance counter
        /// </summary>
        /// <param name="value">Counter value</param>
        public void Set(long value)
        {
            Apply(value);
        }

        /// <summary>
        /// Increments performance counter with the given value. This method doesnot 
        /// apply current pc value to the WMI
        /// </summary>
        /// <param name="value">Increment value</param>
        public void Add(long value)
        {
            this.value += value;
        }

        /// <summary>
        /// Decrements perfoemance counter with the given value
        /// </summary>
        /// <param name="value">Decrement value</param>
        public void Sub(long value)
        {
            this.value -= value;
        }

        /// <summary>
        /// Applies final counter value and resets it
        /// </summary>
        public void Apply()
        {
            Apply(this.value);
            this.value = 0;
        }

        /// <summary>
        /// Applies value to the WMI and resets when specified
        /// </summary>
        /// <param name="reset">Defines if value should be reseted</param>
        public void Apply(bool reset)
        {
            Apply(this.value);

            if (reset)
                this.value = 0;
        }

        #endregion
    }
}
